<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>数据</el-breadcrumb-item>
        <el-breadcrumb-item>书籍</el-breadcrumb-item>
        <el-breadcrumb-item>阅读完成数据</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
      >
        <!-- 自定义按钮 -->
        <template slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="small"
            plain
            @click="handleExcel()"
            >导出Excel</el-button
          >
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { handleExcelCat } from "@/api/exportCommon";
import { common } from "@/api/cat/httpRequest";
import { appStatisticsBookReadFinish as baseUrl } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
        // pageSizes:[1,2,3]
      },
      type: true,
      data: [],
      params: {},
      form: {},
      option: {
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: false,
        selection: true,
        border: true,
        menu: false,
        index: true,
        indexLabel: "序号",
        column: [
          {
            label: "日期",
            prop: "stats_date",
            type: "input",
          },
          {
            label: "书籍id",
            type: "input",
            prop: "book_id",
            search:true
          },
          {
            label: "书籍名称",
            prop: "book_title",
            type: "input",
            search:true
          },
          {
            label: "阅读最后一章人数",
            prop: "read_last_chapter_num",
            type: "input",
          },
          {
            label: "累计阅读人数",
            prop: "total_read_num",
            type: "input",
          },
          {
            label: "全本阅读完读率",
            prop: "read_finish_ratio",
            type: "input",
          },
          {
            label: "日期",
            type: "daterange",
            prop: "daterange",
            hide: true,
            search: true,
          }
        ],
      },
    };
  },
  methods: {
        //数据导出
     async handleExcel() {
       this.loading = true;
       //处理搜索条件
       let params = {...this.$refs.crud.searchForm};
        params.orderByList = "stats_date&DESC,read_last_chapter_num&DESC";

      //日期
      if(params.daterange&&params.daterange.length>0){
        params.stats_date = `!<>!${params.daterange}`;
      }
       delete params.daterange
      //名称模糊搜索
      if(params.book_title){
        params.book_title = `%${params.book_title}%`
      }

       //调用导出接口
       handleExcelCat(
         this.$export,
         baseUrl.pageAndParam,
         { ...params, ...this.default },
         "书籍阅读完成数据统计",
         this.page.total,
         [...this.option.column]
       );
      this.loading = false;
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      this.params.orderByList = "stats_date&DESC,read_last_chapter_num&DESC";
      //发送请求获取数据
      let response = await common(
        `${baseUrl.pageAndParam}`,
        { ...this.params, ...this.defalut },
        this.page,
        "POST"
      );

      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      //日期
      if(params.daterange){
        params.stats_date = `!<>!${params.daterange}`;
      }
      //名称模糊搜索
      if(params.book_title){
        params.book_title = `%${params.book_title}%`
      }
      this.params = params;
      this.page.currentPage = 1;
      delete params.daterange
      this.getList();
      if (done) {
        done();
      }
    },
  },
};
</script>